<?php

namespace app\admin\controller\family;

use app\common\controller\Backend;
use think\Db;

/**
 * 家庭列列表
 *
 * @icon fa fa-circle-o
 */
class Home extends Backend
{

    /**
     * Home模型对象
     * @var \app\admin\model\family\Home
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\Home;
    }

    public function index()
    {
        $group = $this->auth->getGroups();
        $user = $this->auth->getUserInfo();
        $groupId = $group[0]["group_id"];
        $this->assign('groupId', $groupId);
        $this->assignconfig('groupId', $groupId);
        if ($groupId == 1) {
            if ($this->request->isAjax()) {
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
                return json($result);
            }
        } else {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where('id', $user["home_id"])
                ->order($sort, $order)
                ->paginate($limit);
            $this->assign('list', $list[0]);
        }
        return $this->view->fetch();
    }


    public function tuichu()
    {
        $group = $this->auth->getGroups();
        $groupId = $group[0]["group_id"];
        $admin = $this->auth->getUserInfo();
        $row = $this->model->get(['id' => $admin["home_id"]])->toArray();
        if ($groupId == 2) {
            $access = array("group_id" => 4);
            $accesstui = array("group_id" => 2);
            $home_id = array("home_id" =>  null);
            $home_idtui = array("home_id" => $row["id"]);
            $accessEdit = Db::table('jc_auth_group_access')->where("home_id", $admin['home_id'])->update($access);
            if ($accessEdit) {
                $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_id);
                if ($adminEdit) {
                    $homeEdit = $this->model->where("id", $row["id"])->delete();
                    if ($homeEdit) {
                        $this->auth->logout();
                        $this->success('删除成功');
                    } else {
                        $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_idtui);
                        $this->error('删除失败');
                    }
                } else {
                    $accessEdit = Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($accesstui);
                    $this->error('删除失败');
                }
            } else {
                $this->error('删除失败');
            }
        } else {
            $access = array("group_id" => 4);
            $accesstui = array("group_id" => 3);
            $home_id = array("home_id" =>  null);
            $home_idtui = array("home_id" => $row["id"]);
            $home_size = array("home_size" => $row["home_size"] - 1);
            $accessEdit = Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($access);
            if ($accessEdit) {
                $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_id);
                if ($adminEdit) {
                    $homeEdit = $this->model->where("id", $row["id"])->update($home_size);
                    if ($homeEdit) {
                        $this->auth->logout();
                        $this->success('退出成功');
                    } else {
                        $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_idtui);
                        $this->error('退出失败');
                    }
                } else {
                    $accessEdit = Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($accesstui);
                    $this->error('退出失败');
                }
            } else {
                $this->error('退出失败');
            }
        }
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
}
